package com.novellotus.iov.carvice.activity;

import android.os.Bundle;
import android.widget.TextView;

import com.novellotus.httploadingview.LoadingView;
import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.adapter.PayRecordListAdapter;
import com.novellotus.iov.carvice.bean.PointsBean;
import com.novellotus.iov.carvice.bean.RxOrder;
import com.novellotus.iov.carvice.retrofit.BaseEntity;
import com.novellotus.iov.carvice.retrofit.BaseObserver;
import com.novellotus.iov.carvice.retrofit.RetrofitFactory;
import com.novellotus.iov.carvice.retrofit.RxSchedulers;
import com.novellotus.iov.carvice.utils.MyViewUtils;
import com.novellotus.iov.carvice.utils.SharedPreferencesUtils;
import com.novellotus.iov.carvice.view.PageView;
import com.orangegangsters.github.swipyrefreshlayout.library.RefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.RefreshLayoutDirection;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

public class PayRecordListActivity extends BaseActivity implements RefreshLayout.OnRefreshListener, LoadingView.OnBtnClickListener, PageView.PageViewInterface {

    @BindView(R.id.tv_tittle)
    TextView tv_tittle;
    @BindView(R.id.pageView)
    PageView pageView;
    PayRecordListAdapter payRecordListAdapter;

    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_pay_record);
    }

    @Override
    public void init() {
        tv_tittle.setText("消费记录");
        payRecordListAdapter = new PayRecordListAdapter(null,this);
        pageView.refreshLayout.setOnRefreshListener(this);
        pageView.setPageViewInterface(this);
        pageView.setBottomInterface(payRecordListAdapter);
        pageView.setReLoadBtnListener(this);
        pageView.setRecyclerViewAdapter(MyViewUtils.getLinearLayoutManager(this), payRecordListAdapter);
        getList();
    }


    @OnClick(R.id.img_back)
    void back() {
        finish();
    }

    /**
     * 获取积分记录列表
     */
    private void getList() {
        Observable<BaseEntity<RxOrder>> observable = RetrofitFactory.getInstance().orders(SharedPreferencesUtils.getKey(this)[0], pageView.getLoadCurrentPage(), 10);
        observable.compose(RxSchedulers.<BaseEntity<RxOrder>>lifecycle(this))
                .compose(RxSchedulers.<BaseEntity<RxOrder>>compose())
                .subscribe(new BaseObserver<RxOrder>(this) {
                    @Override
                    public void onSuccess(RxOrder response) {
                        pageView.loadSuccess();

                        if (response != null) {
                            if (response.getRecords() == null || response.getRecords().size() == 0) {
                                pageView.loadFail("无相关记录", false);
                            }
                            pageView.setTotalPage(response.getTotal(), 10);
                            if (response.getCurrent() == 1) {
                                payRecordListAdapter.update(response.getRecords());

                            } else {
                                payRecordListAdapter.addData(response.getRecords());
                            }
                          }


                    }

                    @Override
                    public void onFail(int code, String msg) {
                        pageView.loadFail(msg, true);
                    }

                    @Override
                    public void onError(String msg) {
                        pageView.loadFail(msg, true);
                    }

                });
    }

    @Override
    public void loadData(int dataPage) {
        getList();
    }

    @Override
    public void onReload() {
        getList();
    }

    @Override
    public void onRefresh(RefreshLayoutDirection direction) {
        pageView.refreshData(direction);
    }
}
